<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>Document</title>
    <!-- 引入样式 -->
    <link href="../../plugins/element-ui/index.css" rel="stylesheet"/>
    <link href="../../styles/common.css" rel="stylesheet"/>
    <link href="../../styles/page.css" rel="stylesheet"/>
</head>
<body>
<div class="dashboard-container" id="food-app">
    <div class="container">
        <div class="tableBar">
            <el-input
                    @keyup.enter.native="handleQuery"
                    clearable
                    placeholder="请输入请假人名称"
                    style="width: 250px"
                    v-model="input"
            >
                <i
                        @click="init"
                        class="el-input__icon el-icon-search"
                        slot="prefix"
                        style="cursor: pointer"
                ></i>
            </el-input>
            <div class="tableLab">
                <span @click="deleteHandle('批量', null)" class="span-btn delBut non">批量删除</span>
                <span @click="statusHandle('批准')" class="span-btn blueBug non">批量批准</span>
                <span @click="statusHandle('驳回')" class="span-btn delBut non" style="border:none;">批量驳回</span>
                <el-button
                        @click="addFoodtype('add')"
                        type="primary"
                >
                    + 新建假条
                </el-button>
                <el-button
                        @click="exportExcel()"
                        plain
                        type="success"
                >
                    <i class="el-icon-download el-icon--left"></i>
                    导出数据
                </el-button>
            </div>
        </div>
        <el-table
                :data="tableData"
                @selection-change="handleSelectionChange"
                class="tableBox"
                stripe
        >
            <el-table-column
                    type="selection"
                    width="25"
            ></el-table-column>

            <el-table-column
                    label="假条编号"
                    prop="wid"
            ></el-table-column>
            <!--        <el-table-column prop="image" label="图片" align="center">-->
            <!--          <template slot-scope="{ row }">-->
            <!--            <el-image style="width: auto; height: 40px; border:none;cursor: pointer;" -->
            <!--            :src="getImage(row.image)" -->
            <!--            :preview-src-list="[ `/common/download?name=${row.image}` ]" >-->
            <!--            <div slot="error" class="image-slot">-->
            <!--              <img src="./../../images/noImg.png"  style="width: auto; height: 40px; border:none;" >-->
            <!--            </div>  -->
            <!--          </el-image>-->
            <!--          </template>-->
            <!--        </el-table-column>-->
            <el-table-column
                    label="请假人名称"
                    prop="wname"
            ></el-table-column>
            <el-table-column
                    label="请假理由"
                    prop="reason"
            ></el-table-column>
            <el-table-column
                    label="请假时间"
                    prop="vacateTime"
            ></el-table-column>
            <el-table-column
                    label="提交时间"
                    prop="submitTime"
            ></el-table-column>
            <!--        <el-table-column label="售价">-->
            <!--          <template slot-scope="scope">-->
            <!--            <span style="margin-right: 10px;">￥{{ scope.row.price/100 }}</span>-->
            <!--          </template>-->
            <!--        </el-table-column>-->
            <el-table-column label="假条状态">
                <template slot-scope="scope">
                    <span style="margin-right: 10px;">{{scope.row.status}}</span>
                </template>
            </el-table-column>
            <!--        <el-table-column-->
            <!--          prop="updateTime"-->
            <!--          label="最后操作时间"-->
            <!--        >-->
            <!--        </el-table-column>-->
            <!--        -->
            <el-table-column
                    align="center"
                    label="操作"
                    width="160"
            >
                <template slot-scope="scope">
                    <el-button
                            @click="addFoodtype(scope.row.id)"
                            class="blueBug"
                            size="small"
                            type="text"
                    >
                        修改
                    </el-button>
                    <el-button
                            @click="statusHandle(scope.row)"
                            class="blueBug"
                            size="small"
                            type="text"
                    >
                        {{ scope.row.status === '批准' ? '驳回' : '批准' }}
                    </el-button>
                    <el-button
                            @click="deleteHandle('单删', scope.row.wid)"
                            class="delBut non"
                            size="small"
                            type="text"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                :current-page.sync="page"
                :page-size="pageSize"
                :page-sizes="[3, 5, 10, 15]"
                :total="counts"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
                class="pageList"
                layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
    </div>

</div>
<!--    饼状图容器1-->
<div id="container2" style="height: 400px"></div>
<!--    饼状图容器2-->
<!--<div id="container3" style="height: 400px"></div>-->
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/food.js"></script>
<script src="../../js/echarts.min.js"></script>
<script>
    new Vue({
        el: '#food-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 3,
                tableData: [],
                dishState: '',
                checkList: [],
                drawData: []
            }
        },
        computed: {},
        created() {
            this.init()
            //获取图表数据
            this.getDrawData()
        },
        mounted() {
        },
        methods: {
            async init() {
                const params = {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input ? this.input : undefined //兼容模糊查询
                }
                await getDishPage(params).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.list || []
                        this.counts = res.data.total
                    }

                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            getImage(image) {
                return `/common/download?name=${image}`
            },
            //获取图表数据
            async getDrawData() {
                await getAllData().then(res => {
                    if (String(res.code) === '1') {
                        let accessCount = 0
                        let returnCount = 0
                        //统计驳回和批准的个数
                        for (let i = 0; i < res.data.list.length; i++) {
                            if (res.data.list[i].status == '批准') {
                                accessCount++
                            } else {
                                returnCount++
                            }
                        }
                        //初始化数组
                        this.drawData = []
                        //保存到数组
                        this.drawData.push({value: accessCount, name: '批准'}, {value: returnCount, name: '驳回'})
                        //渲染图表
                        this.draw1()
                        // this.draw2()
                        // console.log(this.drawData)
                    }

                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            //模糊查询
            handleQuery() {
                // this.page = 1;
                this.init();
            },
            //请求下载excel表格
            exportExcel() {
                this.$message.success('下载中，请确认下载！')
                const downloadUrl = 'http://localhost:8081/vacate/export';
                // 创建一个隐藏的iframe元素
                const iframe = document.createElement('iframe');
                iframe.style.display = 'none';
                document.body.appendChild(iframe);
                // 设置iframe的src属性，触发下载
                iframe.src = downloadUrl;
            },
            // 添加
            addFoodtype(st) {
                if (st === 'add') {
                    window.parent.menuHandle({
                        id: '4',
                        url: '/page/food/add.html',
                        name: '添加菜品'
                    }, true)
                } else {
                    window.parent.menuHandle({
                        id: '4',
                        url: '/page/food/add.html?id=' + st,
                        name: '修改菜品'
                    }, true)
                }
            },
            //绘制饼状图1
            draw1() {
                console.log("执行绘图了")
                let dom = document.getElementById('container2');
                //‘dark’表示为深色模式，null为浅色
                let myChart = echarts.init(dom, null, {
                    renderer: 'canvas',
                    useDirtyRect: false
                });
                let app = {};

                let option;

                option = {
                    title: {
                        text: '可视化统计',
                        subtext: '假条状态',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left'
                    },
                    series: [
                        {
                            // 调色盘
                            color: [],
                            name: '饼状图',
                            type: 'pie',
                            radius: '50%',
                            data: this.drawData,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };

                if (option && typeof option === 'object') {
                    myChart.setOption(option);
                }

                window.addEventListener('resize', myChart.resize);
            },
            //饼状图2
            draw2() {
                let dom = document.getElementById('container3');
                let myChart = echarts.init(dom, null, {
                    renderer: 'canvas',
                    useDirtyRect: false
                });
                let app = {};

                let option;

                option = {
                    legend: {
                        top: 'bottom'
                    },
                    toolbox: {
                        show: true,
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    series: [
                        {
                            name: 'Nightingale Chart',
                            type: 'pie',
                            radius: [50, 250],
                            center: ['50%', '50%'],
                            roseType: 'area',
                            itemStyle: {
                                borderRadius: 8
                            },
                            data: this.drawData
                        }
                    ]
                };

                if (option && typeof option === 'object') {
                    myChart.setOption(option);
                }

                window.addEventListener('resize', myChart.resize);
            },

            // 删除
            deleteHandle(type, id) {
                if (type === '批量' && id === null) {
                    if (this.checkList.length === 0) {
                        return this.$message.error('请选择删除对象')
                    }
                }
                this.$confirm('确认删除该菜品, 是否继续?', '确定删除', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                }).then(() => {
                    deleteDish(type === '批量' ? this.checkList.join(',') : id).then(res => {
                        if (res.code === 1) {
                            this.$message.success('删除成功！')
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },

            //状态更改
            statusHandle(row) {
                let params = {}
                if (typeof row === 'string') {//多选
                    if (this.checkList.length === 0) {
                        this.$message.error('批量操作，请先勾选操作菜品！')
                        return false
                    }
                    //拼接出多个id
                    params.ids = this.checkList.join(',')
                    params.status = row
                } else {//单选
                    console.log(row)
                    params.ids = row.wid
                    params.status = row.status === "批准" ? '驳回' : '批准'
                }
                // this.dishState = params
                // if (row.status === "批准") {
                //     this.dishState = "驳回"
                // } else {
                //     this.dishState = "批准"
                // }
                // this.dishState=row.status==="批准"?"驳回":"批准"
                this.$confirm('确认更改该假条状态?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    // 起售停售---批量起售停售接口
                    dishStatusByStatus(params.status, params.ids).then(res => {
                        if (res.code === 1) {
                            this.$message.success('状态已经更改成功！')
                            this.handleQuery()
                            //获取最新图表数据
                            this.init()
                            //获取图表数据
                            this.getDrawData()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },

            // 全部操作---获取选中列数据的id
            handleSelectionChange(val) {
                let checkArr = []
                val.forEach((n) => {
                    checkArr.push(n.wid)
                })
                this.checkList = checkArr
                console.log(this.checkList)
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            }
        }
    })
</script>
</body>
</html>